﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using Telerik.Web.UI;

/// <summary>
/// 绑定RadListBox的排序/拖动/移动/删除命令
/// </summary>
public class RadListBoxCommandBinder
{
    private RadListBox sourceBox;
    private RadListBox destBox;
    private MenuInfo menuInfo = new MenuInfo();

    public event EventHandler ActionComplete;

    public RadListBoxCommandBinder(RadListBox sourceBox, RadListBox destBox)
    {
        this.sourceBox = sourceBox;
        this.destBox = destBox;
        this.Init(sourceBox, true);
        this.Init(destBox, false);
    }

    private void Init(RadListBox box, bool isLeft)
    {
        box.AllowDelete = true;
        box.AllowReorder = true;
        if (isLeft)
        {
            box.AllowTransfer = true;
            box.AllowTransferOnDoubleClick = true;
            box.AutoPostBackOnTransfer = true;
            box.Transferring += new RadListBoxTransferringEventHandler(box_Transferring);
        }        

        box.AutoPostBackOnDelete = true;
        box.AutoPostBackOnReorder = true;
        box.EnableDragAndDrop = true;
        box.DataTextField = "m_name";
        box.DataValueField = "m_id";
        box.Culture = CultureInfo.CurrentCulture;
        box.SelectionMode = ListBoxSelectionMode.Multiple;

        box.ItemDataBound += new RadListBoxItemEventHandler(box_ItemDataBound);
        box.Reordered += new RadListBoxEventHandler(box_Reordered);

        box.Transferred += new RadListBoxTransferredEventHandler(box_Transferred);

        box.Deleting += new RadListBoxDeletingEventHandler(box_Deleting);
        box.Deleted += new RadListBoxEventHandler(box_Deleted);

        box.Dropping += new RadListBoxDroppingEventHandler(box_Dropping);
        box.Dropped += new RadListBoxDroppedEventHandler(box_Dropped);
    }

    void box_ItemDataBound(object sender, RadListBoxItemEventArgs e)
    {
        IDataRecord dataRow = (IDataRecord)e.Item.DataItem;
        e.Item.Attributes["isdisabled"] = dataRow["m_disabled"].ToString();
    }

    void box_Reordered(object sender, RadListBoxEventArgs e)
    {
        RadListBox box = sender as RadListBox;
        ChangeItesOrder(box.Items);
    }

    bool IsTransferring
    {
        get
        {
            return sourceBox.Attributes["isTransferring"] == true.ToString();
        }
        set
        {
            sourceBox.Attributes["isTransferring"] = value.ToString();
        }
    }

    void box_Transferring(object sender, RadListBoxTransferringEventArgs e)
    {
        sourceBox.Attributes["isTransferring"] = true.ToString();
    }

    void box_Transferred(object sender, RadListBoxTransferredEventArgs e)
    {
        ChangeItemsDisabled(e.Items);
    }

    void box_Dropping(object sender, RadListBoxDroppingEventArgs e)
    {
        RadListBox box = sender as RadListBox;
        if (e.HtmlElementID != box.ClientID)
            IsTransferring = true;
    }

    void box_Dropped(object sender, RadListBoxDroppedEventArgs e)
    {
        RadListBox box = sender as RadListBox;
        if (e.HtmlElementID != box.ClientID)
        {
            ChangeItemsDisabled(e.SourceDragItems);
            IsTransferring = false;
        }
        else
            ChangeItesOrder(box.Items);
    }

    void box_Deleting(object sender, RadListBoxDeletingEventArgs e)
    {
        if (IsTransferring)
        {
            e.Cancel = true;
            IsTransferring = false;
        }
    }

    void box_Deleted(object sender, RadListBoxEventArgs e)
    {
        foreach (RadListBoxItem item in e.Items)
        {
            menuInfo.Id = int.Parse(item.Value);
            menuInfo.Delete();
        }
        OnActionComplete();
    }

    void ChangeItesOrder(IList<RadListBoxItem> items)
    {
        for (int i = 0; i < items.Count; i++)
        {
            menuInfo.Id = int.Parse(items[i].Value);
            menuInfo.SortIndex = i;
            menuInfo.ChangeSortIndex();
        }
        OnActionComplete();
    }

    void ChangeItemsDisabled(IList<RadListBoxItem> items)
    {
        foreach (RadListBoxItem item in items)
        {
            menuInfo.Id = int.Parse(item.Value);
            menuInfo.Disabled = !Convert.ToBoolean(item.Attributes["isdisabled"]);//0为启用
            menuInfo.ChangeDisable();
        }
        OnActionComplete();
    }

    void OnActionComplete()
    {
        if (ActionComplete != null)
            ActionComplete(this, null);
    }
}